package com.yatube.yygh.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yatube.yygh.common.response.R;
import com.yatube.yygh.common.util.MD5;
import com.yatube.yygh.hospital.service.HospitalSetService;
import com.yatube.yygh.model.hosp.HospitalSet;
import com.yatube.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @author Created with IDEA
 * @description --
 * @date 2024-04-22 15:56
 */
@Tag(name = "医院设置相关接口")
@CrossOrigin // 允许跨域访问
@RestController
@RequestMapping("/admin/hospital/set")
public class HospitalSetController {

    @Resource
    private HospitalSetService hospitalSetService;

    @Operation(summary = "查找所有设置")
    @GetMapping("findAll")
    public List<HospitalSet> findAll(){
        return hospitalSetService.list();
    }

    @Operation(summary = "按id删除设置")
    @DeleteMapping("{id}")
    public Boolean deleteById(@PathVariable("id") Integer id){
        return hospitalSetService.removeById(id);
    }



    @Operation(summary = "分页查询")
    @PostMapping("findPage/{page}/{limit}")
    public R findPage(@PathVariable Integer page,
                      @PathVariable Integer limit,
                      @RequestBody(required = false) HospitalSetQueryVo vo){
        // 创建页面对象
        Page<HospitalSet> queryPage = new Page<>(page, limit);
        // 构建条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(vo.getHosname())) {
            wrapper.like("hosname", vo.getHosname());
        }
        if (StringUtils.isNotEmpty(vo.getHoscode())){
            wrapper.eq("hoscode", vo.getHoscode());
        }
        // get page results
        Page<HospitalSet> pageResult = hospitalSetService.page(queryPage, wrapper);

        return R.ok(pageResult);
    }


    //4 添加医院设置
    @PostMapping("saveHospitalSet")
    public R saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        //设置状态 1 使用 0 不能使用
        hospitalSet.setStatus(1);
        //签名秘钥
        Random random = new Random();
        String signKey = MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000));
        hospitalSet.setSignKey(signKey);
        //调用service
        boolean save = hospitalSetService.save(hospitalSet);
        if(save) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    //5 根据id获取医院设置
    @GetMapping("getHospSet/{id}")
    public R getHospSet(@PathVariable Long id) {
//        try {
//            //模拟异常 测试全局异常
//            int a = 1/0;
//        }catch (Exception e) {
//            throw new ServiceRuntimeException("失败",201);
//        }

        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return R.ok(hospitalSet);
    }

    //6 修改医院设置
    @PostMapping("updateHospitalSet")
    public R updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    //7 批量删除医院设置
    @DeleteMapping("batchRemove")
    public R batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        hospitalSetService.removeByIds(idList);
        return R.ok();
    }

    //8 医院设置锁定和解锁
    @PutMapping("lockHospitalSet/{id}/{status}")
    public R lockHospitalSet(@PathVariable Long id,
                             @PathVariable Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //调用方法
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    //9 发送签名秘钥
    @PutMapping("sendKey/{id}")
    public R lockHospitalSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信
        return R.ok();
    }

}
